package me.luger.project.fof.config.shiro.realm;

import me.luger.project.fof.service.AdminUserService;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractNutRealm extends AuthorizingRealm {

    @Autowired
    protected AdminUserService adminUserService;
//    @Autowired
//    protected RoleDao roleDao;

    /**
     * 更新用户授权信息缓存.
     */
    public void clearCachedAuthorizationInfo(String principal) {
        SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
        clearCachedAuthorizationInfo(principals);
    }

    /**
     * 清除所有用户授权信息缓存.
     */
    public void clearAllCachedAuthorizationInfo() {
        Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
        if (cache != null) {
            for (Object key : cache.keys()) {
                cache.remove(key);
            }
        }
    }

    /**
     * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
     */
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        AdminUser adminUser = (AdminUser) principals.getPrimaryPrincipal();
//        if (!Lang.isEmpty(adminUser)) {
//            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//            info.addRoles(userDao.getRoleCodeList(adminUser));
//            for (Sys_role role : adminUser.getRoles()) {
//                if (!role.isDisabled())
//                    info.addStringPermissions(roleDao.getPermissionNameList(role));
//            }
//            return info;
//        } else {
//            return null;
//        }
//    }
}
